import java.io.BufferedInputStream;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;

import javax.xml.bind.JAXBContext;
import javax.xml.bind.JAXBException;
import javax.xml.bind.Unmarshaller;

import net.sf.json.JSON;
import net.sf.json.xml.XMLSerializer;

import org.apache.http.Header;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.util.EntityUtils;
import org.json.simple.JSONObject;
import org.json.simple.parser.JSONParser;


public class TestRest {
	public static void main(String[] args) throws Exception {

	try {
		 
        // create default HTTP Client
        DefaultHttpClient httpClient = new DefaultHttpClient();

        // Create new getRequest with below mentioned URL
        String wsurl = "https://insurance.oic.or.th/dms_pilot/compulsory_insurance.svc/REST/SendPolicyInformationREST?";
        String ProxyCompanyCode = "";
        String Username = "innova";
        String Password = "abc123";
        String CompanyCode = "2115";
        String PolicyNumber = "DM7244002393001001";
        String ContractDt = "20130409";
        String EffectiveDt = "20130409";
        String EndDt = "20140409";
        String VehicleTypeCode = "1.3";
        String RegisteredProvinceCode = "10";
        String PlateNumber = "กม8583";
        String ChassisVINNumber = "MR053HY420407307";
        String InsuredUniqueId = "1234567890123";
        String InsuredName = "Aswin";
        String InsuredSurname = "Phansap";
        String AgentBrokerLicenseNumber = "5555555555";
        
        String ws = wsurl+"ProxyCompanyCode="+ProxyCompanyCode+'&'+"Username="+Username+'&'+"Password="+Password+'&'+"CompanyCode="+CompanyCode+'&'+"PolicyNumber="+PolicyNumber+'&'+
                "ContractDt="+ContractDt+'&'+"EffectiveDt="+EffectiveDt+'&'+"EndDt="+EndDt+'&'+"VehicleTypeCode="+VehicleTypeCode+'&'+"RegisteredProvinceCode="+RegisteredProvinceCode+'&'+
                "PlateNumber="+PlateNumber+'&'+"ChassisVINNumber="+ChassisVINNumber+'&'+"InsuredUniqueId="+InsuredUniqueId+'&'+"InsuredName="+InsuredName+'&'+"InsuredSurname="+InsuredSurname+'&'+"AgentBrokerLicenseNumber="+AgentBrokerLicenseNumber;
        
        System.out.println("WebserviceURL = ");
        System.out.println(ws);
        
        HttpGet getRequest = new HttpGet(ws);

        
        
        // Add additional header to getRequest which accepts application/xml data
        getRequest.addHeader("accept", "application/xml");

        // Execute your request and catch response
        HttpResponse response = httpClient.execute(getRequest);
        HttpEntity entity = response.getEntity();
        
        
      
        System.out.println("----------------------------------------");
   
        byte[] buffer = new byte[1024];
        if (entity != null) {
          InputStream inputStream = entity.getContent();
          try {
            int bytesRead = 0;
            BufferedInputStream bis = new BufferedInputStream(inputStream);
            while ((bytesRead = bis.read(buffer)) != -1) {
              String chunk = new String(buffer, 0, bytesRead);
              //response from web service
              System.out.println(chunk);
              
              //Convert XML to JSON
              JSON objJson = new XMLSerializer().readObject(chunk);
              System.out.println("JSON data : " + objJson);
              
              //Convert JSON to Object from commande json.get("element name")
              JSONObject json = (JSONObject)new JSONParser().parse(objJson.toString());
              System.out.println("Result= " + json.get("Result"));
              System.out.println("CompanyCode= " + json.get("CompanyCode"));
              System.out.println("Errors= " + json.get("Errors"));
              System.out.println("PolicyNumber= " + json.get("PolicyNumber"));
   
            }
          } catch (IOException ioException) {
           
            ioException.printStackTrace();
          } catch (RuntimeException runtimeException) {
            
            runtimeException.printStackTrace();
          } finally {
            // Closing the input stream will trigger connection release
            try {
              inputStream.close();
            } catch (Exception ignore) {
            }
          }
        }
        

        httpClient.getConnectionManager().shutdown();

    } catch (ClientProtocolException e) {
        e.printStackTrace();

    } catch (IOException e) {
        e.printStackTrace();
    }
}
	
	
}
